﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gtkj.CMS.DALFactory;

namespace Gtkj.CMS.BLL
{
    public abstract class BaseService<T> where T : class,new()
    {
        public DALFactory.DBDataSession GetCurrentSession
        {
            get { return DALFactory.DBDataSessionThreadLonely.CreateDBDataSession(); }
        }
        public DAL.BaseDAL<T> CurrentDal { get; set; }
        public abstract void SetCurrentDal();
        public BaseService()
        {
            SetCurrentDal();
        }

        /// <summary>
        /// 读取实体列表
        /// </summary>
        /// <param name="whereLamdba">查询条件</param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLamdba)
        {
            return CurrentDal.LoadEntities(whereLamdba);
        }

        /// <summary>
        /// 读取实体分页列表
        /// </summary>
        /// <typeparam name="s">排序字段类型</typeparam>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页条数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="whereLamdba">查询条件</param>
        /// <param name="orderLamdba">排序字段</param>
        /// <param name="isAsc">true:升序,false:降序</param>
        /// <returns></returns>where( u=>u.aa==1)
        public IQueryable<T> LoadPagesEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLamdba, System.Linq.Expressions.Expression<Func<T, s>> orderLamdba, bool isAsc)
        {
            return CurrentDal.LoadPagesEntities<s>(pageIndex, pageSize, out totalCount, whereLamdba, orderLamdba, isAsc);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteEntity(T entity)
        {
            CurrentDal.DeleteEntity(entity);
            return this.GetCurrentSession.SaveChange();
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        /// <param name="listEntity"></param>
        /// <returns></returns>
        public bool DeleteMoreEntity(List<T> listEntity)
        {
            foreach (var item in listEntity)
            {
                CurrentDal.DeleteEntity(item);
            }
            return this.GetCurrentSession.SaveChange();
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T AddEntity(T entity)
        {
            CurrentDal.AddEntity(entity);
            this.GetCurrentSession.SaveChange();
            return entity;
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool EditEntity(T entity)
        {
            CurrentDal.EditEntity(entity);
            return this.GetCurrentSession.SaveChange();
        }

        /// <summary>
        /// 更新多条数据
        /// </summary>
        /// <param name="listEntity"></param>
        /// <returns></returns>
        public bool EditMoreEntity(List<T> listEntity)
        {
            foreach (var item in listEntity)
            {
                CurrentDal.EditEntity(item);
            }
            return this.GetCurrentSession.SaveChange();
        }



    }
}
